package zhklappoint.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import zhklappoint.dao.ActivityDao;
import zhklappoint.entity.Activity;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.service.InsertIntoActivityInfoService;
import zhklappoint.util.PageUtils;

@Service
public class InsertIntoActivityInfoServiceImpl  implements InsertIntoActivityInfoService{

	@Autowired
	private ActivityDao cd;
	
	@Override
	public Activity saveActivity(Activity company) {
		
		return cd.save(company);
	}

	@Override
	public void deleteActivity(Activity company) {
		company.setIsdelete(ENUM_ISDELETE.DELETED.value);
		saveActivity(company);
	}

//	@Override
//	public Page<Activity> getActivityList(Activity activtiy, Integer page, Integer size) {
//		activtiy.setIsdelete(ENUM_ISDELETE.NOT_DELETED.value);
//        Pageable pageable= PageUtils.toPageable(page, size, Sort.Direction.DESC, "updatedate");
//        ExampleMatcher matcher=ExampleMatcher.matching()
//                .withMatcher("isdelete", ExampleMatcher.GenericPropertyMatchers.exact())
//                .withMatcher("activityName", ExampleMatcher.GenericPropertyMatchers.contains())
//                .withMatcher("title", ExampleMatcher.GenericPropertyMatchers.contains())
//                .withMatcher("comId", ExampleMatcher.GenericPropertyMatchers.exact())
//                .withIgnorePaths("createdate","updatedate");// 忽略字段，即不管忽略名字是什么值都不加入查询条件
//        Example<Activity> example = Example.of(activtiy, matcher);
//        return cd.findAll(example, pageable);
//	}

	@Override
	public Page<Activity> getActivityList(Activity activtiy, Integer page, Integer size) {
		Pageable pageable = PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("isdelete", ENUM_ISDELETE.NOT_DELETED.value);
		StringBuffer queryString = new StringBuffer("select * from zhkl_appoint_active ");
		queryString.append(" WHERE isdelete = :isdelete ");
		if(!ObjectUtils.isEmpty(activtiy.getComId())) {
			values.put("comId", activtiy.getComId());
			queryString.append(" and com_id = :comId");
		}
		if(!ObjectUtils.isEmpty(activtiy.getTitle())) {
			values.put("title", activtiy.getTitle());
			queryString.append(" and title LIKE '%' :title '%'");
		}
		if(!ObjectUtils.isEmpty(activtiy.getActivityName())) {
			values.put("activityName", activtiy.getActivityName());
			queryString.append(" and activity_name LIKE '%' :activityName '%'");
		}
		queryString.append(" ORDER BY createdate DESC ");
		StringBuffer countString = new StringBuffer("select count(1) from (").append(queryString).append(") a");
		return cd.sqlQuery(queryString+"", countString+"", values, pageable,activtiy.getClass());
	}
	
	@Override
	public Activity getActivityById(String id) {
		Optional<Activity> ou=cd.findById(id);
        if(!ou.isPresent()) {
            return null;
        }
        Activity c=ou.get();
        if(c.getIsdelete()==ENUM_ISDELETE.DELETED.value){
            return null;
        }
        return c;
	}

	@Override
	public Page<Activity> getMyActivityList(String comId, String userId, Integer page, Integer size) {
		Pageable pageable = PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("isdelete", ENUM_ISDELETE.NOT_DELETED.value);
		values.put("userId", userId);
		values.put("comId", comId);
		StringBuffer queryString = new StringBuffer("select a.* from zhkl_appoint_active a ");
		queryString.append(" left join zhkl_user_activity ua on a.id=ua.activ_id ");
		queryString.append(" WHERE a.isdelete = :isdelete ");
		queryString.append(" and ua.isdelete = :isdelete ");
		queryString.append(" and ua.wxuser_id=:userId ");
		queryString.append(" and ua.com_id=:comId ");
		queryString.append(" ORDER BY ua.createdate DESC ");
		StringBuffer countString = new StringBuffer("select count(1) from (").append(queryString).append(") a");
		return cd.sqlQuery(queryString+"", countString+"", values, pageable,new Activity().getClass());
	}
}
